var kt=1;
jQuery(window).scroll(function(e) {
	if(screen.width<=480 || jQuery(".left").height() < jQuery(".right").height()) return;
    var amount;
	//khoảng cách từ bottom lên khi scroll
	kc_bottom = jQuery(document).height()-window.innerHeight-jQuery(window).scrollTop();
   //chiều cao khung phải cách bottom- màn hình vài px
   var topscroll=jQuery(window).scrollTop();
   var kc=50;
   amount=topscroll+window.innerHeight-jQuery(".right").height()-jQuery(".header").height()-kc;
   amount1 =jQuery(document).height()-jQuery(window).scrollTop()-window.innerHeight-jQuery(".bottom").height();
 console.log(jQuery(document).height());
 //  console.log(amount+" | " +amount1);
	var pos=jQuery('.right').position();
	var posbottom=jQuery(document).height()-jQuery(".bottom").height();
	if(amount>=0 && amount1 >-20){
	 if(kt==1)
		{
			console.log("fixed");
			var topval=jQuery(".header").height()-jQuery(window).scrollTop();
			jQuery('.right').css({ "position":"fixed","left":pos.left+"px","top":topval+"px" });
			kt=0;
		}
    }
	else if(amount1 <=-20)
	{
			var scroll=jQuery(document).height()-jQuery(window).scrollTop()-window.innerHeight-jQuery(".bottom").height()-jQuery(".header").height()+kc;
			var topval=(scroll-(jQuery(".right").height()-window.innerHeight-kc));
			jQuery('.right').css({ "position":"fixed","left":pos.left+"px","top":topval+"px" });
	}
	else{
		jQuery('.right').css({ "position":""});
		kt=1;
    }
});
